package cn.demomaster.quickpicturer.helper;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.demomaster.quickpicturer.model.Folder;
import cn.demomaster.quickpicturer.model.ImageInfo;
import com.demomaster.quickcompat.util.QDFileUtil;

/**
 * Created by Squirrel桓 on 2018/11/28.
 */
public class PictureManager {

    private static final String TAG = "PictureManager";

    /**
     * 从SDCard加载图片
     */
    public static void loadImageForSDCard(final Context context, final DataCallback callback) {
        //由于扫描图片是耗时的操作，所以要在子线程处理。
        new Thread(() -> {
            //扫描图片
            Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            ContentResolver mContentResolver = context.getContentResolver();
            Cursor mCursor = mContentResolver.query(mImageUri, new String[]{
                            MediaStore.Images.Media.DATA,
                            MediaStore.Images.Media.DISPLAY_NAME,
                            MediaStore.Images.Media.DATE_ADDED,
                            MediaStore.Images.Media._ID},
                    null,
                    null,
                    MediaStore.Images.Media.DATE_ADDED);

            ArrayList<ImageInfo> images = new ArrayList<>();
            //读取扫描到的图片
            while (mCursor.moveToNext()) {
//                // 获取图片的路径
//                String path = mCursor.getString(
//                        mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
//                //获取图片名称
//                String name = mCursor.getString(
//                        mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
//                //获取图片时间
//                long time = mCursor.getLong(
//                        mCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
                long id = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media._ID));

                Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                ImageInfo image = new ImageInfo(imageUri);//path, time, name);
                //image.setUri(imageUri);
                images.add(image);
            }
            mCursor.close();
            Log.d(TAG, "images.size():" + images.size());
            Collections.reverse(images);
            callback.onSuccess(splitFolder(images));
        }).start();
    }

    private static final LinkedHashMap<String, Folder> folderMap = new LinkedHashMap<>();

    /**
     * 把图片按文件夹拆分，第一个文件夹保存所有的图片
     */
    private static ArrayList<Folder> splitFolder(ArrayList<ImageInfo> images1) {
        ArrayList<Folder> folders = new ArrayList<>();
        ArrayList<ImageInfo> images = new ArrayList<>();
        for (int i = 0; i < images1.size(); i++) {
            if (images1.get(i) != null) {
                images.add(images1.get(i));
            }
        }
        folderMap.put("全部图片", new Folder("全部图片", images));
        if (!images.isEmpty()) {
            int size = images.size();
            for (int i = 0; i < size; i++) {
                Uri uri = images.get(i).getUri();
                //  Log.d("CGQ",i+" image path"+path);
                String name = QDFileUtil.getFolderName(uri);
                if (!TextUtils.isEmpty(name)) {
                    if (images.get(i) != null) {
                        getFolder(name, images.get(i));
                    }
                    //folder.addImage();
                }
            }
        }
        for (Map.Entry entry : folderMap.entrySet()) {
            folders.add((Folder) entry.getValue());
        }
        return folders;
    }

    private static void getFolder(String name, ImageInfo image) {
        if (!folderMap.containsKey(name)) {
            Log.i("CGQ", "containsKey=" + name);
            Folder folder = new Folder(name);
            folder.addImage(image);
            folderMap.put(name, folder);
        } else {
            Folder folder = folderMap.get(name);
            folder.addImage(image);
            folderMap.put(name, folder);
        }
    }

    public interface DataCallback {
        void onSuccess(ArrayList<Folder> folders);
    }

}
